#!/usr/bin/python
import argparse

parser = argparse.ArgumentParser(description="Process PTS files. Print"
	" PASS/INC/FAIL tests")

parser.add_argument("-c", "--csv", action='store_true',
                     help="format as a CSV")

parser.add_argument("-mf", "--multiplefiles",
			nargs='+',
			required=True,
			type=argparse.FileType('r'),
			help="Process given multiple files")

parser.add_argument("-e", "--exclude", action='store_true',
			help="Print results only for profiles/protocols containing errors")

#def profileName(str):
#	"Function should provide profile name"
#	print str
#	return

#def profileExecDate(str):
#	"Function should return date of execution"
#	print str
#	return


try:
	results = parser.parse_args()
except IOError, msg:
	parser.error(str(msg))

args = parser.parse_args()

pts_issue_literal = "PTS issue"
our_issue_literal = "JIRA"
profile_name =""
pts_version = ""
android_version = ""

for (i, item) in enumerate(args.multiplefiles):
	line = args.multiplefiles[i].readline()

	tests_total = 0
	passed = -1
	inconclusive = -1
	failed = -1
	not_available = -1
	pts_issue = 0
	our_issue = 0

	if args.csv:
		while line != "":
			if "PTS test results" in line:
				profile_name = line
				listP = line.split(' ', 8)
				profile = listP[4].rstrip('\n')
			if "PTS version:" in line:
				pts_version = line
				pts_v_tmp = line.split(' ', 3)
				pts =  pts_v_tmp[2].rstrip('\n')
			if "Android version:" in line:
				android_version = line
				android_v_tmp = line.split(' ', 3)
				android =  android_v_tmp[2].rstrip('\n')
			if "TC_" in line:
				tests_total += 1
			if "PASS" in line:
				passed += 1
			if "INC" in line:
				inconclusive += 1
			if "FAIL" in line:
				failed += 1
			if "N/A" in line:
				not_available += 1
			if pts_issue_literal in line:
				pts_issue += 1
			if our_issue_literal in line:
				our_issue += 1
			line = args.multiplefiles[i].readline()
		available = (passed + inconclusive + failed)
		line.rstrip('\r\n')
		print("%s,%d,%d,%d,%d,%d,%d,%d,%d,,%s,%s" %(profile, tests_total, passed, inconclusive, failed, available, not_available, pts_issue, our_issue, pts, android))
	elif args.exclude:
		while line != "":
			if "PTS test results" in line:
				profile_name = line
				listP = line.split(' ', 8)
				profile = listP[4].rstrip('\n')
			if "TC_" in line:
				tests_total += 1
			if "PASS" in line:
				passed += 1
			if "INC" in line:
				inconclusive += 1
			if "FAIL" in line:
				failed += 1
			if "N/A" in line:
				not_available += 1
			if pts_issue_literal in line:
				pts_issue += 1
			if our_issue_literal in line:
				our_issue += 1
			line = args.multiplefiles[i].readline()
		if pts_issue or our_issue != 0:
			print("\n")
			print("\n_____________________________________________")
			print("%s" %profile)
                	print("Total number of tests: %d" %tests_total)
                	print("\tPASS: %d" %passed)
                	print("\tINC: %d" %inconclusive)
                	print("\tFAIL: %d" %failed)
                	available = (passed + inconclusive + failed)
                	print("\tAvailable: %d" %available)
                	print("\tN/A: %d" %not_available)
                	print(".........................")
                	print("Total PTS issues: %d" %pts_issue)
                	print("Total of our issues: %d" %our_issue)
                	print("\n")
	else:
		while line != "":
			if "PTS test results" in line:
				print("\n_____________________________________________")
				print(" " + line),
				profile_name = line
			if "PTS version:" in line:
				print("   " + line),
			if "Tested:" in line:
				print("   " + line),
			if "Android version:" in line:
				print("   " + line),
				print("............................................."),
			if "TC_" in line:
				tests_total += 1
			if "PASS" in line:
				passed += 1
			if "INC" in line:
				inconclusive += 1
			if "FAIL" in line:
				failed += 1
			if "N/A" in line:
				not_available += 1
			if pts_issue_literal in line:
				pts_issue += 1
			if our_issue_literal in line:
				our_issue += 1
			line = args.multiplefiles[i].readline()

		print("\n")
		print("Total number of tests: %d" %tests_total)
		print("\tPASS: %d" %passed)
		print("\tINC: %d" %inconclusive)
		print("\tFAIL: %d" %failed)
		available = (passed + inconclusive + failed)
		print("\tAvailable: %d" %available)
		print("\tN/A: %d" %not_available)
		print(".........................")
		print("Total PTS issues: %d" %pts_issue)
		print("Total of our issues: %d" %our_issue)
		print("\n")

		total_check = passed + inconclusive + failed + not_available
		correct = tests_total - total_check
		if correct != 0:
			print("Be warned!!! Something is fucked up with the numbers!")
			print("\"Total number of tests\" != (PASS + INC + FAIL + N/A)\n")
			print("\tPASS + INC + FAIL + N/A = %d \n" %total_check)
			print("\tShould be = %d \n" %tests_total)

if args.csv:
	print(",total,PASS,INC,FAIL,AVAILABLE,N/A,PTS issues,OUR issues,,pts version,android version\n")
print("Keep in mind: not all our issues are having JIRA bug report!")
print("For this reason sum of \"Total PTS issues\" and \"Total of our")
print("issues\" might not be equal INC + FAIL")
print("There might be some other corrner cases i.e. more than once PTS issue")
print("for one test! So use common sense and if in doubts check the results")
print("against corresponding pts-xxx.txt")
print("\n")
